package com.os.sys.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.os.sys.mapper.SysRoleMapper;
import com.os.sys.pojo.SysRole;
import com.os.util.Tools;
import com.os.util.page.PageRequest;
import com.os.util.page.PageResult;
import com.os.util.page.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    /**
     * 采用分页插件进行分页
     * @param pageRequest
     * @param map
     * @return
     */
    public PageResult selectByPage(PageRequest pageRequest, Map map) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> sysUserList = sysRoleMapper.selectByParam(map);
        Tools.indexOrder(sysUserList,"role_index");
        PageInfo pageInfo = new PageInfo<SysRole>(sysUserList);
        return PageUtils.getPageResult(pageRequest, pageInfo);
    }
    /**
     * 按ID查询角色
     * @param id
     * @return
     */
    public SysRole selectById(int id){
        return sysRoleMapper.selectById(id);
    }

    public List<SysRole> selectByRole_Ids(String role_ids){
        return sysRoleMapper.selectByRole_Ids(role_ids);
    }
    /**
     * 新增角色
     * @param sysRole
     */
    public void insert(SysRole sysRole){
        //生成唯一ID
        sysRoleMapper.insert(sysRole);
    }
    /**
     * 修改角色
     * @param sysRole
     */
    public void update(SysRole sysRole){
        sysRoleMapper.update(sysRole);
    }

    /**
     * 按ID删除角色
     * @param id
     */
    public void deleteById(int id){
        sysRoleMapper.deleteById(id);
    }
}

